(function () {
    // 等待所有类都加载完成
    function initializeApp() {
        try {
            // 检查所有必需的类是否已加载
            if (!window.xflib ||
                !window.xflib.client ||
                !window.xflib.client.Product ||
                !window.xflib.client.ProductService ||
                !window.xflib.client.ProductUi ||
                !window.xflib.client.ProductModal ||
                !window.xflib.client.Category ||
                !window.xflib.client.CategoryService ||
                !window.xflib.client.CategoryUi ||
                !window.xflib.client.CategoryModal ||
                !window.xflib.client.Project ||
                !window.xflib.client.ProjectService ||
                !window.xflib.client.ProjectUi ||
                !window.xflib.client.ProjectModal
            ) {
                // 如果有类未加载完成，等待100ms后重试
                setTimeout(initializeApp, 100);
                return;
            }
            //App = window.xflib.client.Application;

            const storage = new xflib.client.Storage(false, true);

            const productService = new xflib.client.ProductService(storage);
            const productUi = new xflib.client.ProductUi(productService);

            const categoryService = new xflib.client.CategoryService(storage);
            const categoryUi = new xflib.client.CategoryUi(categoryService);

            const projectService = new xflib.client.ProjectService(storage);
            const projectUi = new xflib.client.ProjectUi(projectService, categoryService, productService);

            // 注册服务到Application
            window.xflib.client.Application.registerBean("projectService", projectService);
            window.xflib.client.Application.registerBean("categoryService", categoryService);
            window.xflib.client.Application.registerBean("productService", productService);

            window.xflib.client.Application.registerBean("productUi", productUi);
            window.xflib.client.Application.registerBean("categoryUi", categoryUi);
            window.xflib.client.Application.registerBean("projectUi", projectUi);

            // 初始化界面
            window.xflib.client.Application.refreshUi();

            // 打开help模态框
            document.getElementById('helperBtn').addEventListener('click', (e) => {
                e.stopPropagation();
                const modal = document.getElementById('help-modal');
                modal.style.display = modal.style.display === 'none' ? 'block' : 'none';
            });

            // 点击help模态框外部关闭help模态框
            window.onclick = function (event) {
                const modal = document.getElementById('help-modal');
                if (!event.target.closest('#help-modal-content') && modal.style.display !== 'none') {
                    modal.style.display = 'none';
                }
            }

            // 配置可拖拽的弹窗
            dragElement(document.getElementById('help-modal'))
            dragElement(document.getElementById('project-modal'))
            dragElement(document.getElementById('category-modal'))
            dragElement(document.getElementById('product-modal'))

        } catch (error) {
            console.error('初始化应用失败:', error);
        }
    }

    // 当 DOM 加载完成后开始初始化
    document.addEventListener('DOMContentLoaded', initializeApp);

})();
